// 制作项目相关工具类:  公用函数
//手机号码
function phone(phoneNum) {
    let retelnum = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
    return retelnum.test(phoneNum)
}
//密码
function pwd(pwdv) {
    let repsd = /^[a-zA-Z]\w{5,17}$/;
    return repsd.test(pwdv)
}
// 页脚部分追加ul
function jumpPage(checkPage) {
    let ul = document.createElement('ul')
    ul.className = 'd_f bottommenu tcenter';
    ul.innerHTML = `  <li><a href="./homePage.html" class="d_f fdcolumn ${checkPage=='homePage'?'active':''}"><span class="icon-home iconfont"></span><span>首页</span></a>
    </li>
    <li><a href="./run.html" class="d_f fdcolumn ${checkPage=='run'?'active':''}"><span class="icon-sports iconfont"></span><span
                >运动</span></a></li>
    <li><a href="./circle.html" class="d_f fdcolumn ${checkPage=='circle'?'active':''}"><span class="icon-instrument iconfont"></span><span
                >圈子</span></a></li>
    <li><a href="./mine.html" class="d_f fdcolumn ${checkPage=='mine'?'active':''}"><span class="iconfont icon-mine"></span><span>我的</span></a>
    </li>`
    // 将创建的ul添加到body中
    document.body.appendChild(ul)
}
//页头部分追加ul

function checkPage(a) {
    let header = document.querySelector('.comHeader')
    let ul = document.createElement('ul')
    ul.className = 'd_f'
    ul.innerHTML = `<li class="tcenter ${a=='run'?'active':''}"><a href="./run.html">跑步</a> </li> 
    <li class="tcenter ${a=='rideBycle'?'active':''}"><a href="./rideBycle.html">骑行</a> </li> 
    <li class="tcenter ${a=='courseList'?'active':''}"><a href="./courseList.html">课程训练</a> </li> `
    // 将创建的ul添加到header中
    header.appendChild(ul)
}
//判定用户是否登录
function ifLogin() {
    let userData = window.localStorage.getItem('userData')
    return userData ? true : false
}
//不足10补0
function zeroNum(d) {
    return d < 10 ? '0' + d : d
}

//  根据经纬度计算距离
//  参数：第一个点的经纬度   &   第二个点的经纬度

function calcDistance(lat1, lng1, lat2, lng2) {
    var radLat1 = lat1 * Math.PI / 180.0;
    var radLat2 = lat2 * Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    return s.toFixed(2) // 单位千米
}

/* 
  计算 “配速” 函数
  参数d:  距离
  参数s： 花费的时间  
*/
function calcPace(d, s) {
    let distance = d
    let spendTime = s / 60
    if (distance > 0 && spendTime > 0) {
        let pace = parseFloat(spendTime / distance)
        let m = Math.floor(pace)
        let s = ((pace - m) * 60).toFixed(0)
        return `${m}'${s}"`
    }
    return "--"
}

/* 
  计算卡路里
  参数: 跑步的距离
  calCalorie(miles)
*/
function calCalorie(miles) {
    if (miles > 0) {
        let calorie = 60 * miles * 1.036;
        return calorie.toFixed(2)
    }
    return "--"
}

/* 添加标记函数 */
function addMarker(map, point, iconName) {
    // 创建图标对象   
    const icon = new BMap.Icon(iconName, new BMap.Size(45, 45), {
        anchor: new BMap.Size(15, 30), // 设置大小
        imageSize: new BMap.Size(30, 32) // 设置图片大小  
    });
    const marker = new BMap.Marker(point, {
        icon: icon
    });
    map.addOverlay(marker);
    return marker
}

/* 绘制线条函数 */
function drawLine(map, curPoint, nextPoint) {
    const polyline = new BMap.Polyline([
        new BMap.Point(curPoint.lng, curPoint.lat),
        new BMap.Point(nextPoint.lng, nextPoint.lat)
    ], {
        strokeColor: "blue",
        strokeWeight: 6
    });
    map.addOverlay(polyline);
}


// 处理时间格式
function formatTime(a) {
    let h = Math.floor(a / 3600);
    let m = Math.floor((a / 60) % 60);
    let s = a % 60
    let time = [h, m, s].map(function (v) {
        return zeroNum(v)
    }).join(':');
    return time

}

//暴露
window.utils = {
    phone: phone,
    pwd: pwd,
    jumpPage: jumpPage,
    ifLogin: ifLogin,
    checkPage: checkPage,
    zeroNum: zeroNum,
    //地图相关
    drawLine: drawLine,
    addMarker: addMarker, 
    calCalorie: calCalorie,
    calcPace: calcPace,
    calcDistance: calcDistance,
    formatTime: formatTime
}